package com.by.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by.mapper.UserinfoMapper;
import com.by.entity.Userinfo;
import com.by.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * (Userinfo)表服务实现类
 *
 * @author 白也
 * @since 2024-01-17 16:17:12
 */
@Service("userinfoService")
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService {

    @Autowired
    private UserinfoMapper userinfoMapper;

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @Override
    public Userinfo login(String username, String password) {
        LambdaQueryWrapper<Userinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Userinfo::getUsername,username).eq(Userinfo::getPassword,password);
        return userinfoMapper.selectOne(wrapper);
    }

    /**
     * 注册用户
     *
     * @param userinfo 用户实体类
     * @return true/false
     */
    @Override
    public boolean register(Userinfo userinfo) {
        return userinfoMapper.insert(userinfo) > 0;
    }

    /**
     * 找回密码验证
     * @param username
     * @param email
     * @return
     */
    @Override
    public Boolean findPass(String username, String email) {
        LambdaQueryWrapper<Userinfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Userinfo::getUsername,username).eq(Userinfo::getEmail,email);
        return userinfoMapper.selectOne(wrapper) != null;
    }

    /**
     * 按照名字更新密码
     *
     * @param username 用户名
     * @param password 密码
     * @return true/false
     */
    @Override
    public boolean updatePassWord(String password,String username) {
        LambdaQueryWrapper<Userinfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Userinfo::getUsername, username);
        Userinfo userinfo = new Userinfo();
        userinfo.setPassword(password);
        return userinfoMapper.update(userinfo,queryWrapper) > 0;
    }

    @Override
    public boolean usernameCheck(String username) {
        LambdaQueryWrapper<Userinfo> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.eq(Userinfo::getUsername,username);
        return userinfoMapper.selectOne(Wrapper)!=null;
    }
}

